package com.qyb.socket;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

/**
 * <p>
 * 实现功能：
 * <p/>
 *
 * @author 秦有宝
 * @version 1.0.00
 */
@ServerEndpoint("/echo")
public class EchoSocket {

    private String userId;
    public EchoSocket() {
        System.out.println("Constructor");
    }

    @OnOpen
    public void open(Session session){
        //一个Session代表一个通信会话
        System.out.println("sessionId："+session.getId());
        String queryString = session.getQueryString();
        System.out.println(queryString);
        userId = queryString.split("=")[1];
        System.out.println(userId);
//        session.getPathParameters();
    }



    @OnClose
    public void close(Session session){
        //一个Session代表一个通信会话

        System.out.println("sessionId："+session.getId()+"close");
        try {
            session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @OnMessage
    public void message(Session session, String msg){
        System.out.println("客户端："+ msg);
        try {
            session.getBasicRemote().sendText("服务器：你好");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
